package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.dto.audit.AuditConfirmHistoryDto;
import com.yunyao.common.web.util.ConvertUtil;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.common.web.util.YResult;
import com.yunyao.dao.model.BamuAuditConfirmHistory;
import com.yunyao.dao.service.bamu.IBamuAuditConfirmHistoryService;
import com.yunyao.framework.service.AuditConfirmHistoryService;
import com.yunyao.framework.vo.audit.AuditConfirmHistoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenyejian
 * @Date 2024/8/16 19:38
 */
@Service
public class AuditConfirmHistoryServiceImpl implements AuditConfirmHistoryService {
    @Autowired
    private IBamuAuditConfirmHistoryService iBamuAuditConfirmHistoryService;
    @Override
    public YResult<List<AuditConfirmHistoryVO>> getHistoryListByBizId(AuditConfirmHistoryDto dto) {
        LambdaQueryWrapper<BamuAuditConfirmHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BamuAuditConfirmHistory::getBizId, dto.getBizId())
                .eq(StringUtils.isNotBlank(dto.getType()), BamuAuditConfirmHistory::getType, dto.getType())
                .eq(StringUtils.isNotBlank(dto.getStatus()), BamuAuditConfirmHistory::getStatus, dto.getStatus())
                .eq(BamuAuditConfirmHistory::getIsDeleted, DataDeletedStatus.NO.getType());
        List<BamuAuditConfirmHistory> list = iBamuAuditConfirmHistoryService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return YResult.success();
        }
        return YResult.success(list.stream().map(this::convertDao2Vo).collect(Collectors.toList()));
    }

    private AuditConfirmHistoryVO convertDao2Vo(BamuAuditConfirmHistory bamuAuditConfirmHistory) {
        if (bamuAuditConfirmHistory != null) {
            AuditConfirmHistoryVO auditConfirmHistoryVO = new AuditConfirmHistoryVO();
            BeanUtils.copyProperties(bamuAuditConfirmHistory, auditConfirmHistoryVO);
            if(bamuAuditConfirmHistory.getStatus() != null){
                auditConfirmHistoryVO.setStatusName(ConvertUtil.null2String(bamuAuditConfirmHistory.getStatus()));
            }
            return auditConfirmHistoryVO;
        }
        return null;
    }


}
